import xml.etree.ElementTree as ET

# 解析 XML 文件

def parse_xml(file_path):
    tree = ET.parse(file_path)
    root = tree.getroot()
    return root

def parse_get_form(root):

    form_node = root.find(".//Elements/Form")
    if form_node is None:
        print("未找到 <form> 节点")
        return []
    id = form_node.findtext("Id", default=None)
    name = form_node.findtext("Name", default=None)
    print(id, name)

def parse_get_field(root):

    # 找到 <Elements> 节点
    elements_node = root.find(".//Elements")
    if elements_node is None:
        print("未找到 <Elements> 节点")
        return []

    # 存储结果
    results = []

    # 遍历 <Elements> 下的所有子节点
    for child in elements_node:
        # 检查标签是否以 "Field" 结尾
        if child.tag.endswith("Field"):
            # 检查是否有 action="edit" 属性
            if child.attrib.get("action") == "edit":
                continue  # 跳过带有 action="edit" 的标签
            ElementType = child.attrib.get("ElementType")
            # 提取需要的字段
            entity_key = child.findtext("EntityKey", default=None)
            field_name = child.findtext("FieldName", default=None)
            name = child.findtext("Name", default=None)
            id_value = child.findtext("Id", default=None)
            key = child.findtext("Key", default=None)

            # 将结果保存为字典
            result = {
                "ElementType": ElementType,
                "EntityKey": entity_key,
                "FieldName": field_name,
                "Name": name,
                "Id": id_value,
                "Key": key,
            }
            results.append(result)

    return results

def parse_get_entity(root):
    results = []

    # 找到 <Elements> 节点
    elements_node = root.find(".//Elements")
    if elements_node is None:
        print("未找到 <Elements> 节点")
        return []
    # 遍历 <Elements> 下的所有子节点
    for child in elements_node:
        # 检查标签是否以 "Field" 结尾
        if child.tag.endswith("Entity"):
            tag = child.tag.split(":")[-1]
            EntryName = child.findtext("EntryName", default=None)
            EntryPkFieldName = child.findtext("EntryPkFieldName", default=None)
            TableName = child.findtext("TableName", default=None)
            Name = child.findtext("Name", default=None)
            Id = child.findtext("Id", default=None)
            Key = child.findtext("Key", default=None)
            ElementType = child.findtext("ElementType", default=None)
            results.append({
                "tag": tag,
                "EntryName": EntryName,
                "EntryPkFieldName": EntryPkFieldName,
                "TableName": TableName,
                "Name":Name,
                "Id": Id,
                "Key": Key,
                "ElementType": ElementType,
            })
    return results

# 示例：调用函数并打印结果
if __name__ == "__main__":
    file_path = "采购入库单.xml"  # 替换为你的 XML 文件路径
    root = parse_xml(file_path)
    parse_get_form(root)
    extracted_data = parse_get_field(root)
    for item in extracted_data:
        print(item)
    print(len(extracted_data))

    file_path = "采购入库单.xml"  # 替换为你的 XML 文件路径
    extracted_data = parse_get_entity(root)
    for item in extracted_data:
        print(item)
    print(len(extracted_data))